package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Zjxm;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class ZjxmForm extends BasePage implements Serializable {
    private GenericManager<Zjxm, Long> zjxmManager;
    private Zjxm zjxm = new Zjxm();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setZjxmManager(GenericManager<Zjxm, Long> zjxmManager) {
        this.zjxmManager = zjxmManager;
    }

    public Zjxm getZjxm() {
        return zjxm;
    }

    public void setZjxm(Zjxm zjxm) {
        this.zjxm = zjxm;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        zjxmManager.remove(zjxm.getId());
        addMessage("zjxm.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            zjxm = zjxmManager.get(id);
        } else {
            zjxm = new Zjxm();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (zjxm.getId() == null || zjxm.getId() == 0);
        // set the related jbqk as a foreign key.
        zjxm.setJbqk( jbqk );
        zjxmManager.save(zjxm);

        String key = (isNew) ? "zjxm.added" : "zjxm.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 